<template>
	<view class="page-container">
		<!-- 技术记录内容区 -->
		<view class="main-content">
			<view class="container">
				<!-- 技术记录主体 -->
				<view class="article-main">
					<!-- 特色技术记录样式的头部 -->
					<view class="featured-article-header">
						<!-- 推荐标签 -->
						<view class="recommended-badge" v-if="article.isRecommended">
							<text>推荐</text>
						</view>

						<!-- 技术记录标题 -->
						<text class="article-title">{{ article.title }}</text>

						<!-- 技术记录元信息 -->
						<view class="article-meta">
							<text class="category">{{ article.category }}</text>
							<text class="date">{{ formatDate(article.date) }}</text>
							<view class="author">
								<image src="https://picsum.photos/id/1005/100/100" mode="aspectFill" class="avatar">
								</image>
								<text class="author-name">何俊东</text>
							</view>
						</view>

						<!-- 技术记录封面 -->
						<view class="article-cover" v-if="article.coverUrl">
							<image :src="article.coverUrl" mode="widthFix" :alt="article.title + '的封面图'"></image>
						</view>

						<!-- 技术记录摘要 -->
						<view class="article-excerpt" v-if="article.excerpt">
							<view v-html="article.contentHtml"></view>
						</view>
					</view>
				</view>

				<!-- 侧边栏 -->
				<view class="sidebar">
					<!-- 作者信息 -->
					<AuthorInfo></AuthorInfo>

					<!-- 推荐阅读 -->
					<view class="sidebar-card recommended-articles" v-if="recommendedArticles.length != 0">
						<text class="card-title">推荐记录</text>
						<view class="recommended-list">
							<view class="recommended-item" v-for="(item, index) in recommendedArticles" :key="index" @click="myRedirectTo(`/subPackages1/pages/article/look?id=${item._id}`)">
								<image :src="item.coverUrl" mode="aspectFill" class="recommended-img"></image>
								<text class="recommended-title">{{ item.title }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 相关技术记录 -->
		<view class="related-articles" v-if="relatedArticles.length != 0">
			<view class="container">
				<text class="section-title">相关技术</text>
				<view class="articles-grid">
					<view class="article-card" v-for="(item, index) in relatedArticles" :key="index" @click="myRedirectTo(`/subPackages1/pages/article/look?id=${item._id}`)">
						<image :src="item.coverUrl" mode="aspectFill" class="article-card-img"></image>
						<text class="article-card-title">{{ item.title }}</text>
						<view class="article-card-meta">
							<text class="category">{{ item.category }}</text>
							<text class="date">{{ item.date }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import AuthorInfo from '../../../components/authorInfo/index.vue'
	export default {
		components: {
			AuthorInfo
		},
		data() {
			return {
				articleId: null,
				article: {
					title: '',
					category: '',
					date: '',
					isRecommended: false,
					coverUrl: '',
					excerpt: '',
					contentHtml: '',
					tags: []
				},
				recommendedArticles: [],
				relatedArticles: []
			};
		},

		onLoad(options) {
			if (options.id) {
				this.articleId = options.id;
				this.loadArticleDetail();
			}
			
		},

		methods: {
			// 加载技术记录详情
			async loadArticleDetail() {
				const submitData = {
					databaseName: 'articles',
					state: 'look',
					_id: this.articleId
				};

				let result = await this.$axios('overallSituation', submitData , true)

				if (result.code == 200 && result.data.length > 0) {
					this.article = result.data[0] || {};
					this.getSameType()
					this.getRecommend()
				} else {
					uni.showToast({
						title: '加载技术记录失败',
						icon: 'none'
					});
					setTimeout(() => {
						uni.navigateBack();
					}, 1500);
				}
			},
			
			// 获取推荐阅读的技术记录
			async getRecommend(){
				const submitData = {
					databaseName: 'articles',
					state: 'look',
					isRecommendedName:'是',
					status:'发布'
				};
				
				let result = await this.$axios('overallSituation', submitData)
				if(result.code == 200){
					this.recommendedArticles = result.data.filter(item => item._id != this.articleId)
				}
			},
			
			// 获取同类型技术记录
			async getSameType(){
				const submitData = {
					databaseName: 'articles',
					state: 'look',
					category:this.article.category,
					status:'发布'
				};
				
				let result = await this.$axios('overallSituation', submitData)
				if(result.code == 200){
					this.relatedArticles = result.data.filter(item => item._id != this.articleId)
				}
			},

			// 格式化日期
			formatDate(dateString) {
				if (!dateString) return '';

				const date = new Date(dateString);
				return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
			},

			// 返回列表
			handleBack() {
				uni.navigateBack();
			},
			
			myRedirectTo(url){
				uni.redirectTo({
				  url: url // 目标页面的路径
				});
			},

			// 编辑技术记录
			handleEdit() {
				this.$router(`/subPackages1/pages/article/edit?id=${this.articleId}`);
			}
		}
	};
</script>

<style scoped>
	/* 基础样式 */
	.page-container {
		font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica, sans-serif;
		background-color: #f5f5f5;
		color: #333;
		min-height: 100vh;
	}

	.container {
		width: 98%;
		padding: 0 16px;
		margin: 0 auto;
		box-sizing: border-box;
	}

	/* 导航栏样式 */
	.navbar {
		background-color: #fff;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
		position: sticky;
		top: 0;
		z-index: 99;
	}

	.navbar-content {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 44px;
		padding: 0 16px;
		box-sizing: border-box;
	}

	.back-btn,
	.edit-btn {
		width: 44px;
		height: 44px;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #6B7280;
		font-size: 18px;
	}

	.back-btn:active,
	.edit-btn:active {
		background-color: #f5f5f7;
		border-radius: 50%;
	}

	.nav-title {
		font-size: 18px;
		font-weight: 600;
		color: #1F2937;
	}

	/* 主内容区样式 */
	.main-content {
		padding: 20px 0;
		box-sizing: border-box;
	}

	.container {
		display: flex;
		flex-direction: column;
		gap: 30px;
	}

	/* 技术记录主体样式 */
	.article-main {
		flex: 1;
	}

	.featured-article-header {
		background-color: #fff;
		border-radius: 8px;
		overflow: hidden;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
		margin-bottom: 30px;
		padding: 20px;
		position: relative;
	}

	/* 推荐标签 */
	.recommended-badge {
		position: absolute;
		top: 20px;
		right: 20px;
		background-color: #FF9500;
		color: #FFFFFF;
		font-size: 12px;
		padding: 3px 8px;
		border-radius: 4px;
	}

	/* 技术记录标题 */
	.article-title {
		font-size: 24px;
		font-weight: bold;
		color: #1F2937;
		line-height: 1.3;
		margin-bottom: 16px;
		display: block;
	}

	/* 技术记录元信息 */
	.article-meta {
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		gap: 12px;
		margin-bottom: 20px;
	}

	.category {
		font-size: 12px;
		padding: 3px 8px;
		background-color: #EBF4FF;
		color: #3B82F6;
		border-radius: 4px;
	}

	.date {
		font-size: 12px;
		color: #6B7280;
	}

	.author {
		display: flex;
		align-items: center;
		gap: 8px;
		margin-left: auto;
	}

	.avatar {
		width: 24px;
		height: 24px;
		border-radius: 50%;
	}

	.author-name {
		font-size: 12px;
		color: #1F2937;
	}

	/* 技术记录封面 */
	.article-cover {
		width: 100%;
		border-radius: 8px;
		overflow: hidden;
		margin-bottom: 20px;
	}

	.article-cover image {
		width: 100%;
		display: block;
	}

	/* 技术记录摘要 */
	.article-excerpt {
		font-size: 14px;
		color: #4B5563;
		line-height: 1.6;
		padding: 16px;
		background-color: #F9FAFB;
		border-radius: 8px;
		margin-bottom: 10px;
		font-style: italic;
		display: block;
	}

	/* 技术记录内容 */
	.article-content {
		background-color: #fff;
		border-radius: 8px;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
		padding: 20px;
		margin-bottom: 30px;
		font-size: 16px;
		line-height: 1.8;
		color: #1F2937;
	}

	.article-content p {
		margin-bottom: 20px;
	}

	.article-content h1,
	.article-content h2,
	.article-content h3 {
		font-weight: bold;
		margin: 30px 0 15px;
		color: #111827;
	}

	.article-content h1 {
		font-size: 24px;
	}

	.article-content h2 {
		font-size: 20px;
	}

	.article-content h3 {
		font-size: 18px;
	}

	.article-content img {
		max-width: 100%;
		height: auto;
		border-radius: 8px;
		margin: 20px 0;
		display: block;
	}

	.article-content ul,
	.article-content ol {
		margin: 15px 0 15px 20px;
	}

	.article-content li {
		margin-bottom: 10px;
	}

	.article-content blockquote {
		border-left: 3px solid #3B82F6;
		padding-left: 15px;
		margin: 20px 0;
		color: #6B7280;
		font-style: italic;
	}

	/* 技术记录底部信息 */
	.article-footer {
		background-color: #fff;
		border-radius: 8px;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
		padding: 20px;
		margin-bottom: 30px;
	}

	.article-tags {
		display: flex;
		flex-wrap: wrap;
		gap: 8px;
		margin-bottom: 16px;
	}

	.tag {
		font-size: 12px;
		padding: 4px 10px;
		background-color: #F3F4F6;
		color: #4B5563;
		border-radius: 20px;
	}

	.article-actions {
		display: flex;
		gap: 20px;
	}

	.action-item {
		display: flex;
		align-items: center;
		gap: 6px;
		color: #6B7280;
		font-size: 14px;
	}

	.action-icon {
		font-size: 16px;
	}

	/* 侧边栏样式 */
	.sidebar {
		display: flex;
		flex-direction: column;
		gap: 20px;
	}

	.sidebar-card {
		background-color: #fff;
		border-radius: 8px;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
		padding: 20px;
		box-sizing: border-box;
	}

	.author-card {
		text-align: center;
	}

	.author-avatar {
		width: 80px;
		height: 80px;
		border-radius: 50%;
		margin: 0 auto 16px;
		border: 4px solid rgba(59, 130, 246, 0.1);
	}

	.author-name {
		font-size: 18px;
		font-weight: bold;
		color: #1F2937;
		display: block;
		margin-bottom: 8px;
	}

	.author-title {
		font-size: 14px;
		color: #6B7280;
		display: block;
		margin-bottom: 12px;
	}

	.author-bio {
		font-size: 14px;
		color: #4B5563;
		line-height: 1.6;
		margin-bottom: 16px;
		display: block;
	}

	.social-links {
		display: flex;
		justify-content: center;
		gap: 12px;
	}

	.social-icon {
		width: 36px;
		height: 36px;
		border-radius: 50%;
		background-color: #F3F4F6;
		color: #6B7280;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 16px;
	}

	.card-title {
		font-size: 16px;
		font-weight: bold;
		color: #1F2937;
		margin-bottom: 16px;
		display: block;
	}

	/* 推荐阅读 */
	.recommended-list {
		display: flex;
		flex-direction: column;
		gap: 16px;
	}

	.recommended-item {
		display: flex;
		gap: 12px;
	}

	.recommended-img {
		width: 80px;
		height: 80px;
		border-radius: 6px;
		flex-shrink: 0;
	}

	.recommended-title {
		font-size: 14px;
		color: #1F2937;
		line-height: 1.4;
		display: -webkit-box;
		-webkit-line-clamp: 3;
		-webkit-box-orient: vertical;
		overflow: hidden;
	}

	/* 相关技术记录 */
	.related-articles {
		background-color: #F9FAFB;
		padding: 30px 0;
		box-sizing: border-box;
	}

	.section-title {
		font-size: 20px;
		font-weight: bold;
		color: #1F2937;
		display: block;
	}

	.articles-grid {
		display: grid;
		grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
		gap: 20px;
	}

	.article-card {
		background-color: #fff;
		border-radius: 8px;
		overflow: hidden;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
	}

	.article-card-img {
		width: 100%;
		height: 160px;
	}

	.article-card-title {
		font-size: 16px;
		font-weight: 600;
		color: #1F2937;
		padding: 16px 16px 0;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		overflow: hidden;
		line-height: 1.4;
	}

	.article-card-meta {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 12px 16px 16px;
		font-size: 12px;
		color: #6B7280;
	}

	/* 响应式调整 */
	@media (min-width: 768px) {
		.container {
			flex-direction: row;
		}

		.sidebar {
			width: 300px;
			flex-shrink: 0;
		}
	}
</style>